﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using WebApplication.AppCode;
using BLL;
using Model;

namespace WebApplication
{
    public partial class ProjectTimeGraph : BasePage
    {
        AppBLL bll = new AppBLL();
        protected string MapData = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            Guid id;

            if (!Guid.TryParse(Request["id"], out id))
            {
                AlertDataNotFound(); return;
            }
            LoadData(id);
        }

        private void LoadData(Guid id)
        {
            var model = bll.GetProjectViewByID(id);

            var map = GetMap(model);
            map.MapCSS = "Map";
            map.NodeStatusCSS.Add(NodeStatusEnum.Completed, "Completed");
            map.NodeStatusCSS.Add(NodeStatusEnum.Ignore, "Ignore");
            map.NodeStatusCSS.Add(NodeStatusEnum.Waiting, "Waiting");
            map.NodeStatusCSS.Add(NodeStatusEnum.Working, "Working");
            map.WorkNodeGeneralCSS = "Node";
            map.AreaCtontentCSS = "AreaName";
            map.AreaTitleCSS = "AreaTitle";
            MapContainer.InnerHtml = map.GetTimeHtml();
            MapData = map.JsonData;
        }

        private MapView GetMap(ProjectView model)
        {
            MapView map = new MapView(model);
            map.Name = model.Project.Name;
            map.Title = "项目报建流程";
            model.Areas.ForEach(x =>
            {
                Area area = new Area { ID = x.AreaID, Name = x.Name, Order = x.Order };
                var nodes = model.Nodes.Where(w => w.AreaID == area.ID).ToList();
                nodes.ForEach(n =>
                {
                    WorkNode node = new WorkNode { AreaID = area.ID, Content = n.Content, ID = n.ID, Order = n.Order, Status = n.Status, BeginDate = n.BeginDate, EndDate = n.EndDate };
                    var nextNodes = from f in model.Relationships.Where(w => w.NodeID == n.ID) select f.NextNodeID;
                    node.NextNodeIDList.AddRange(nextNodes);
                    area.Nodes.Add(node);
                });
                map.Areas.Add(area);
            });


            model.Nodes.ForEach(x =>
            {

            });

            return map;
        }
    }
}
